﻿

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Reflection;
using System.Web;

namespace Viewer.BusinessObject.BusinessEntities
{
    [Serializable()]
    public partial class NSKhenThuong : BusinessBase
    {
        NSQuyetDinh quyetDinh = new NSQuyetDinh();
        CommonCode common = new CommonCode();
        DataClassesDataContext db = new DataClassesDataContext();
        public string MaSo { get; set; }
        public void EditKT(int hdId, DateTime nam, string ghiChu, string tenKhenThuong, string hinhThuc, int qdID)
        {
            List<NSKhenThuong> lst = (List<NSKhenThuong>)HttpContext.Current.Session["KT"];
            NSKhenThuong pc = lst.Where(a => a.KhenThuongId.Equals(hdId)).SingleOrDefault();
            pc.TenKhenThuong = tenKhenThuong;
            pc.GhiChu = ghiChu;
            if (!nam.Year.Equals(1))
            {
                pc.NgayKhenThuong = nam;
            }
            pc.LoaiKhenThuong = hinhThuc;
            //chỉ thêm mới khi quyết định > 0.
            //Đã tồn tại qdID khi =0
            if (qdID > 0)
            {
                pc.QuyetDinhID = qdID;
                pc.MaSo = quyetDinh.GetMaSo(qdID);
            }
            else
            {
                pc.MaSo = quyetDinh.GetMaSo(pc.QuyetDinhID.Value);
            }
        }
        public void DeleteKT(int hdId)
        {
            List<NSKhenThuong> lst = (List<NSKhenThuong>)HttpContext.Current.Session["KT"];
            NSKhenThuong pc = lst.Where(a => a.KhenThuongId.Equals(hdId)).SingleOrDefault();
            lst.Remove(pc);
        }
        public void DeleletAllKTByNhanVien(int nhanVienId)
        {
            var query = (from a in db.NSKhenThuongs
                         where a.NhanVienID.Equals(nhanVienId)
                         select a).ToList();
            if (query.Count > 0)
            {
                db.NSKhenThuongs.DeleteAllOnSubmit(query);
                db.SubmitChanges();
            }

        }
        public void UpdateKT_NhanVien(int nhanVienId, List<NSKhenThuong> nblObject)
        {

            var queryNBLByNhanVienId = (from a in db.NSKhenThuongs
                                        where a.NhanVienID.Equals(nhanVienId)
                                        select a).ToList();
            if (queryNBLByNhanVienId.Count > 0)
            {
                db.NSKhenThuongs.DeleteAllOnSubmit(queryNBLByNhanVienId);
                db.SubmitChanges();
            }

            foreach (NSKhenThuong pc in nblObject)
            {
                NSKhenThuong item = new NSKhenThuong();
                CopyNSKhenThuong(pc, item);
                db.NSKhenThuongs.InsertOnSubmit(item);
            }
            db.SubmitChanges();

        }
        private void CopyNSKhenThuong(NSKhenThuong src, NSKhenThuong des)
        {
            des.NgayKhenThuong = src.NgayKhenThuong;
            des.TenKhenThuong = src.TenKhenThuong;
            des.GhiChu = src.GhiChu;
            des.NhanVienID = src.NhanVienID;
            des.LoaiKhenThuong = src.LoaiKhenThuong;
            des.QuyetDinhID = src.QuyetDinhID;

        }
        public List<NSKhenThuong> LoadKTByNhanVienId(int nhanVienId)
        {

            var query = (from a in db.NSKhenThuongs
                         where a.NhanVienID.Equals(nhanVienId)
                         orderby a.TenKhenThuong descending
                         select a).ToList();
            foreach (NSKhenThuong item in query)
            {              
                item.MaSo = item.QuyetDinhID.HasValue ? quyetDinh.GetMaSo(item.QuyetDinhID.Value) : string.Empty;
            }
            return query;

        }
        public NSKhenThuong GetKhenThuong(int nhanVienId, DateTime ngay, string tenKhenThuong, string hinhThucKhenThuong, string ghiChu,int qdID)
        {
            var db = new DataClassesDataContext();
            NSKhenThuong hd = new NSKhenThuong();
            hd.KhenThuongId = 0 - DateTime.Now.Second;
            if (!ngay.Year.Equals(1))
            {
                hd.NgayKhenThuong = ngay;
            }
            hd.TenKhenThuong = tenKhenThuong;
            hd.GhiChu = ghiChu;
            hd.NhanVienID = nhanVienId;
            hd.LoaiKhenThuong = hinhThucKhenThuong;
            hd.QuyetDinhID = qdID;
            hd.MaSo = quyetDinh.GetMaSo(qdID);
            return hd;
        }
    }
}